package com.example.module_hp_zao_yin_media.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import java.lang.reflect.ParameterizedType;

public abstract class BaseMvvmActivity<T extends ViewDataBinding,X extends ViewModel> extends AppCompatActivity {
    public Bundle mSavedInstanceState;
    public Context mContext;
    protected T binding;
    protected X viewModel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        mSavedInstanceState = savedInstanceState;
        binding = DataBindingUtil.setContentView(this, initLayout());
        Class<X> xClass = (Class<X>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        viewModel = new ViewModelProvider(this).get(xClass);
        binding.setLifecycleOwner(this);
        binding.setVariable(initBR(),viewModel);
        //设置沉浸式状态栏背景透明
        //BaseUtils.setStatusBarLucency(this);
        //设置状态栏背景白色
        initView();
    }
    //设置layout
    protected abstract int initLayout();
    //设置数据id,文件直接return BR.data;
    protected abstract int initBR();
    //初始化视图
    protected abstract void initView();
    /*公共方法*/
    //Toast弹窗
    public void showToast(String msg) {
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
    }
    //Toast异步弹窗
    public void showToastSync(String msg) {
        if(Looper.myLooper()==null){
            Looper.prepare();
        }
        Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
        Looper.loop();
    }
    //Activity跳转
    public void navigateTo(Class cls) {
        Intent in = new Intent(mContext, cls);
        startActivity(in);
    }
    //Activity设置传参跳转
    public void navigateToWithBundle(Class cls, Bundle bundle) {
        Intent in = new Intent(mContext, cls);
        in.putExtras(bundle);
        startActivity(in);
    }
    //Activity设置Flag跳转
    public void navigateToWithFlag(Class cls, int flags) {
        Intent in = new Intent(mContext, cls);
        in.setFlags(flags);
        startActivity(in);
    }
}